package b1.scenes.fight
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.utils.BFont;
	
	import com.greensock.TweenLite;
	import com.greensock.easing.Bounce;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;

	public class EndPane extends Sprite
	{
		public var buttonOK:BasicButton;
		public var messageText:TextField;
		private var mainSprite:Sprite;
		private var tipOfEquip:Sprite;
		
		//resultType:作案结果,-2完败； -1惜败； 1胜利； 2险胜；3完胜 4秒杀
		// myBonesPrice：骨头, addExp:增加的经验, heirloomFlag:是否失去传家骨
		public function EndPane(fightResultType:int, myBonesPrice:int, addExpNum:int, loseHeirloomId:int, bossEquipsDrop:Object)
		{
			super();
			var MessageBoxGgr:Class = ResourceHelper.getInstance().getClassByName("EndPaneBgr");
			mainSprite = new MessageBoxGgr();
			mainSprite.width = 450;
			mainSprite.height = 340;
			mainSprite.alpha = 0.95;
			addChild(mainSprite);
			if (fightResultType < 0)
			{
				this.filters = [new ColorMatrixFilter(BFont.blackWhiteFilter)];
			}
			
			//确定按钮
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr3");
			buttonOK = new BasicButton(ButtonBgr, "确定");
			buttonOK.x = (mainSprite.width  - buttonOK.width) / 2;
			buttonOK.y = mainSprite.height - buttonOK.height - 60;
			addChild(buttonOK);
			//显示结束文字信息
			showResultMessages(fightResultType, myBonesPrice, addExpNum, loseHeirloomId, bossEquipsDrop);
		}
		
		//显示战斗结果
		private function showResultMessages(fightResultType:int, myBonesPrice:int, addExpNum:int, loseHeirloomId:int, bossEquipsDrop:Object):void
		{
			var tempString:String = "";
			var titleString:String = "战绩";
			var extraString:String = "";
			if (fightResultType == 4) 
			{//秒杀
				extraString += "秒杀！";
				extraString += "系统奖励和经验翻" + Config.expRatioForPerfectWin.toFixed(0) + "倍！ \n";
				addResultWords("WordMiao", "WordSha");
			}
			else if (fightResultType == 3)
			{//完胜
				extraString += "完胜！";
				extraString += "系统奖励和经验翻" + Config.expRatioForGoodWin.toFixed(0) + "倍！ \n";
				addResultWords("WordWan", "WordSheng");
			}
			else if (fightResultType == 2)
			{//险胜
				extraString += "险胜！";
				extraString += "系统奖励和经验翻" +  Config.expRatioForLittleWin.toFixed(0)  + "倍！ \n";
				addResultWords("WordXian", "WordSheng");
			}
			else if (fightResultType == 1)
			{//胜利
				extraString += "胜利！\n";
				addResultWords("WordSheng");
			}
		
			else if (fightResultType == -2)
			{//完败
				extraString += "完败！\n";
				addResultWords("WordWan", "WordBai");
			}
			else if (fightResultType == -1)
			{//惜败
				extraString += "惜败！\n";
				addResultWords("WordXi", "WordBai");
			}
			tempString += "骨头: " + myBonesPrice + "\n经验: "+ addExpNum + "\n";
			
			if (loseHeirloomId >= 0)
			{
				tempString += "赢得对方的传家骨！\n";
			}
			//标题
		 	var titleText:TextField = new TextField();
			titleText.autoSize = TextFieldAutoSize.CENTER;
			titleText.selectable = false;
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 20, 0x000000);
			titleText.text = titleString;
			titleText.x = (mainSprite.width - titleText.width) / 2 - 40;
			titleText.y = 40;
			addChild(titleText);
		
			//其他解释说明信息
			var posX:int = titleText.x;
			var extraText:TextField = new TextField();
	    	extraText.autoSize = TextFieldAutoSize.LEFT;
	    	extraText.selectable = false;
			extraText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			extraText.text = extraString;
			var fight:Fight = SceneContainer.getInstance().currentScene as Fight;
			var leftAnimalInfo:AnimalInfo = PlayerInfoHelper.getInstance().animals[fight.leftAnimal.animalId];
			var leftAnimalConfig:AnimalConfig = Config.animalConfigs[leftAnimalInfo.animalIndex];
			if (leftAnimalConfig.camp == Clock.currentSeason)
			{
				extraText.appendText(Clock.seasonNames[Clock.currentSeason] + " 经验增加 " + Config.expBonusForDayNight * 100 + "%!");
			}
			extraText.width = 250;
			extraText.wordWrap = true;
			extraText.x = posX;
			extraText.y = 180;
			addChild(extraText);
			
			//战绩
			var textStr:String;
			var equipPosY:int;
			if (loseHeirloomId >= 0)
			{
				textStr = "1";
				//骨头
				addResultItem(posX, 110, "骨头", "PriceBone", myBonesPrice.toString());
				//经验
				addResultItem(posX, 140, "经验", "ExpIcon", addExpNum.toString());
				equipPosY = 170;
				//传家骨
				addResultItem(posX, 80, "传家骨", "Heirloom", textStr, true);
			}
			else
			{
				//骨头
				addResultItem(posX, 80, "骨头", "PriceBone", myBonesPrice.toString());
				//经验
				addResultItem(posX, 110, "经验", "ExpIcon", addExpNum.toString());
				equipPosY = 140;
			}
			//装备
			if (fightResultType > 0)
			{
				var count:int = 0;
				for (var equipIndex:String in bossEquipsDrop)
				{
					var equipConfig:EquipConfig = Config.objectConfigs[equipIndex];
					var EquipIcon:Class = ResourceHelper.getInstance().getClassByName(equipConfig.iconImage);
					var equipIcon:BasicButton = new BasicButton(EquipIcon, null, 15, equipConfig);
					var equipIconContainer:Sprite = new Sprite();
					equipIcon.width = 20;
					equipIcon.height = 20;
					equipIcon.x = - equipIcon.width / 2;
					equipIcon.y = - equipIcon.height / 2;
					equipIconContainer.addChild(equipIcon);
					//设置装备发光
					if (equipConfig.color > 0)
					{
						var gradientGlow:GradientGlowFilter = new GradientGlowFilter();
						gradientGlow.distance = 0;
						gradientGlow.angle = 45;
						gradientGlow.colors = [0xFFFFFF, Config.equipColor2[equipConfig.color]];
						gradientGlow.alphas = [0, 1];
						gradientGlow.ratios = [0, 255];
						gradientGlow.blurX = 20;
						gradientGlow.blurY = 20;
						gradientGlow.strength = 2;
						gradientGlow.quality = BitmapFilterQuality.HIGH;
						gradientGlow.type = BitmapFilterType.OUTER;
						equipIconContainer.filters = [gradientGlow];
					}
					var targeX:Number = posX + 55 + 30 * count;
					var targeY:Number = equipPosY + 28 - 10;
					var delayTime:int = count * 2;
					TweenLite.delayedCall(delayTime, ShowIconMovie, [equipIconContainer, targeX, targeY]);
					equipIcon.addEventListener(MouseEvent.MOUSE_OVER, onEquipIconMouseOver);
					equipIcon.addEventListener(MouseEvent.MOUSE_OUT, onEquipIconMouseOut);
					count++;
				}
				if (count != 0)
				{
					var equipText:TextField = new TextField();
			    	equipText.autoSize = TextFieldAutoSize.LEFT;
			    	equipText.selectable = false;
					equipText.defaultTextFormat = new TextFormat("微软雅黑", 18, 0x000000);
					equipText.text = "装备";
					equipText.x = posX;
					equipText.y = equipPosY;
					addChild(equipText);
				}
			}
		}
		
		//显示装备的动画,（图片位于中心点）
		private function ShowIconMovie(icon:Sprite, targeX:Number, targeY:Number):void
		{
			var iconWidth:int = 20;
			var iconScaleX:Number = iconWidth / icon.width;
			var iconScaleY:Number = 20 / icon.height;
			addChild(icon);
			icon.x = (mainSprite.width - icon.width) / 2;
			icon.y = (mainSprite.height - icon.height) / 2;
			icon.width = 200;
			icon.height = 200;
			TweenLite.to(icon, 1.5, {x:targeX, y:targeY, scaleX:iconScaleX, scaleY:iconScaleY, rotation: 0, ease:Bounce.easeIn});
		
		}

		//装备的信息
		private function onEquipIconMouseOver(event:MouseEvent):void
		{
			var equipIcon:BasicButton = event.target.parent as BasicButton;
			var equipConfig:EquipConfig = equipIcon.userData as EquipConfig;
			tipOfEquip = new Sprite();
			var roundRec:RoundRec = new RoundRec(120, 170, 0.8, 0x50AF50);
			tipOfEquip.addChild(roundRec);
			
			//名字
			var nameText:TextField = new TextField();
			nameText.selectable = false;
        	nameText.width = roundRec.width;
        	nameText.autoSize = TextFieldAutoSize.LEFT;
			var nameTextFormat:TextFormat = new TextFormat("微软雅黑", 15, 0x000000, true);
			nameTextFormat.leftMargin = 8;
			nameTextFormat.rightMargin = 8;
			nameText.defaultTextFormat = nameTextFormat;
			nameText.y = 5;
			tipOfEquip.addChild(nameText);

			//具体功能介绍
			var description:TextField = new TextField();
			description.selectable = false;
        	description.autoSize = TextFieldAutoSize.LEFT;
        	description.width = roundRec.width;
        	description.wordWrap = true;
			var textFormat:TextFormat = new TextFormat("微软雅黑", 13, 0x000000);
			textFormat.leftMargin = 10;
			textFormat.rightMargin = 10;
			description.defaultTextFormat = textFormat;
			if (equipConfig.durability == -1)
			{
				description.text = "耐久度: 永久";	
			}
			else
			{
				description.text = "耐久度: " + equipConfig.durability;	
			}
			description.appendText("\n作用: " + equipConfig.description);
			description.y = nameText.y + 20;
			tipOfEquip.addChild(description);

			var suitEffect:TextField = new TextField();
			suitEffect.selectable = false;
        	suitEffect.autoSize = TextFieldAutoSize.LEFT;
        	suitEffect.width = roundRec.width;
        	suitEffect.wordWrap = true;
			suitEffect.defaultTextFormat = textFormat;
			suitEffect.text = "整套附加: " + equipConfig.wholeEquipDescription.toString();
			
			if (equipConfig.color > 0)
			{
				var colorText:TextField = new TextField();
				colorText.selectable = false;
	        	colorText.autoSize = TextFieldAutoSize.LEFT;
	        	colorText.width = roundRec.width;
	        	colorText.wordWrap = true;
			
				var colorTextFormat:TextFormat = new TextFormat("微软雅黑", 13, Config.equipColor1[equipConfig.color], true);
				colorTextFormat.leftMargin = 8;
				colorTextFormat.rightMargin = 8;
				colorText.y = description.y + description.height - 5;
				colorText.defaultTextFormat = colorTextFormat;
				colorText.text = "品质:" + Config.equipColorName[equipConfig.color] + " " + equipConfig.colorDescribe;
				var colorNameFormat:TextFormat = new TextFormat("微软雅黑", 15, Config.equipColor1[equipConfig.color], true);
				colorNameFormat.leftMargin = 8;
				colorNameFormat.rightMargin = 8;
				nameText.defaultTextFormat = colorNameFormat;
				suitEffect.y = colorText.y + colorText.height - 5 ;
				tipOfEquip.addChild(colorText);
			}
			else
			{
				suitEffect.y = description.y + description.height - 5;
			}
			nameText.text = equipConfig.name;
			tipOfEquip.addChild(suitEffect);
			
			tipOfEquip.x = equipIcon.parent.x + equipIcon.x;
			tipOfEquip.y = equipIcon.parent.y + equipIcon.y - tipOfEquip.height;
			addChild(tipOfEquip);
		}
		
		private function onEquipIconMouseOut(event:MouseEvent):void
		{
			if (null !=  tipOfEquip && null != tipOfEquip.parent)
        	{
        		tipOfEquip.parent.removeChild(tipOfEquip);
        	}
		}
		
		//添加右侧的战斗结果的信息
		private function addResultItem(posX:int, posY:int, leftWords:String, iconStr:String, rightWords:String = null, iconMcFlag:Boolean = false):void
		{
			var iconWidth:int = 20;
			//左侧文字
			var leftText:TextField = new TextField();
	    	leftText.autoSize = TextFieldAutoSize.LEFT;
	    	leftText.selectable = false;
			leftText.defaultTextFormat = new TextFormat("微软雅黑", 18, 0x000000);
			leftText.text = leftWords;
			leftText.x = posX;
			leftText.y = posY;
			addChild(leftText);
			//右侧文字
			if (null != rightWords)
			{
				var rightText:TextField = new TextField();
				rightText.selectable = false;
		    	rightText.autoSize = TextFieldAutoSize.LEFT;
				rightText.defaultTextFormat = new TextFormat("微软雅黑", 18, 0x000000);
				rightText.text = rightWords;
				rightText.x = leftText.x + leftText.width + iconWidth + 10;
				rightText.y = leftText.y;
				addChild(rightText);
			}
			//中间图标
			if (iconMcFlag)
			{
				var IconMc:Class = ResourceHelper.getInstance().getClassByName(iconStr);
				var iconMc:BasicMc = new BasicMc(IconMc);
				var scale:Number = iconWidth / iconMc.width;
				var targeX:Number = leftText.x + leftText.width + 5;
				var targeY:Number = leftText.y +(leftText.height - iconMc.height * scale) / 2;
				ShowIconMovie(iconMc, targeX, targeY);
			}
			else
			{
				var Icon:Class = ResourceHelper.getInstance().getClassByName(iconStr);
				var icon:Sprite = new Icon();
				icon.width = iconWidth;
				icon.scaleY = icon.scaleX;
				icon.x = leftText.x + leftText.width + 5;;
				icon.y = leftText.y +(leftText.height - icon.height) / 2;
				addChild(icon);
			}
		}
		
		//添加左边作案结果“胜“”败”等文字
		private function addResultWords(upWordStr:String, downWordStr:String = null):void
		{
			var wordPosX:int;
			var wordPosY:int;
			var wordWidth:int;
			var UpWord:Class = ResourceHelper.getInstance().getClassByName(upWordStr);
			var upWord:Sprite = new UpWord();
			
			if (null == downWordStr)
			{
				upWord.width = 120;
				upWord.scaleY = upWord.scaleX;
				upWord.x = 30;
				upWord.y = ( mainSprite.height - upWord.height ) / 2 - 20;
			}
			else
			{
				var DownWord:Class  = ResourceHelper.getInstance().getClassByName(downWordStr);
				var downWord:Sprite = new DownWord();
				downWord.width = 100;
				downWord.scaleY = downWord.scaleX;
				
				upWord.width = 100;
				upWord.scaleY = upWord.scaleX;
				upWord.x = 25;
				upWord.y = ( mainSprite.height - upWord.height - upWord.height - 10 ) / 2 - 20;//25;
				
				downWord.x = upWord.x;
				downWord.y = upWord.y + upWord.height + 10;
				addChild(downWord);
			}
			addChild(upWord);
		}
		
  }
}